package com.phoxell.admin;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.phoxell.DataImg;
import com.phoxell.PageFrame;
import com.phoxell.PageServlet;
import com.phoxell.Serial;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings("serial")
public class AdminServlet extends PageServlet{
	public void doGet(HttpServletRequest req,PageFrame page)throws Exception{
		UserService us=UserServiceFactory.getUserService();
		if(us.getCurrentUser()==null || !us.isUserAdmin()){
			page.sendRedirect(us.createLoginURL(req.getRequestURI()));
			return;
		}
		String p=req.getPathInfo();
		if(p!=null){
			p=p.substring(1);
			if(p.length()>0){
				printOne(p,page,us.createLogoutURL("/"));
		        return;
			}
		}
		printAll(page,us.createLogoutURL("/"));
	}
	private void printAll(PageFrame page,String signOut){
        page.title="管理";
        page.aside=null;
		page.tools="<ul id=tools><li><a href=\""+signOut+
				"\">登出</a><li><a href=/admin/>刷新</a></ul>";
		page.print("<table class=list>");
		page.print("<tr><th class=w040><td>Steps<td>Fware<td>Flags<td>Time");
		Query q=new Query("i24").addSort("t",SortDirection.DESCENDING);
		DatastoreService d=DatastoreServiceFactory.getDatastoreService();
		PreparedQuery u=d.prepare(q);
		int n=50;
		for(Entity result:u.asIterable())try{
			String i=(String)result.getProperty("p");
			page.print("<tr><th><a href=/kalkr/");
			page.print(i);
			page.print(">");
			page.print(i);
			page.print("</a><td>");
			page.printNz((Long)result.getProperty("stp"));
			long x=(Long)result.getProperty("fmw");
			page.print(x<4?"<td class=gray>":"<td>");
			page.print(String.format("%x.%02x",x>>8,(byte)x));
			page.print("<td>");
			page.printNz((Long)result.getProperty("flg"));
			page.print("<td><a href=/admin/");
			page.print(i);
			page.print(" class=z2 t=");
			Long t=(Long)result.getProperty("t");
			page.print(t/1000);
			page.print("></a>");
			if(n%5==1)
				page.print("<tr style=height:1ex>");
			if(--n==0)
				break;
		}catch(Exception e){
			e.printStackTrace();
		}
		page.print("</table>");
	}
	private void printOne(String id,PageFrame page,String signOut)throws Exception{
        page.title="信息";
        page.aside=null;
		page.tools="<ul id=tools><li><a href=\""+signOut+
				"\">登出</a><li><a href=/admin/>管理</a></ul>";
		Serial a=new Serial(id);
		a.update(false);
		page.print("<br><h4>序列号</h4>");
		page.print(id.length()<32?id:a.getFormat());
		page.print("<h4>固件版本</h4>");
		short v=a.getFirmware();
		page.print(String.format("%x.%02x",v>>8,v&0xff));
		page.print("<br><br><h4>同步器</h4>");
		page.print(a.getAgent());
		page.print("<br><br>");
		DataImg i=new DataImg(id);
		i.outBits2(page);
	}
}